package com.besttone.action;

import com.besttone.bean.BusinessTypeByDayBean;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.util.HbaseClientUtil;
import com.besttone.util.Sms2SolrUtil;
import com.besttone.util.SolrObject;
import com.jfinal.core.Controller;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrDocumentList;

import java.util.List;

/**
 * 87330083 10-24 11-15
 * Created by zhengzhenzhen on 2016/11/15.
 */
public class BusinessTypeByDayAction extends Controller {
    private static Log _log = LogFactory.getLog(SmsAction.class);

    /**
     * 离线按天按业务类型统计(短信数、短链接访问数、消息数...)
     */
    public void queryByDay() {
        String send_parent_account="bstjm_gj";//default
        String streaming_no = getPara("streaming_no");
        String sp_account = getPara("sp_account");
        String send_number = getPara("send_number");// 发送号码
        String day_id_from = getPara("day_id_from");// 发送时间开始：按天
        String day_id_to = getPara("day_id_to");// 发送时间结束：按天
        String busi_type=getPara("busi_type");//短信SMS;闪信FLASH_SMS;通话记录CALL_RECORD;短链接SHORT_LINK
        String template_code=getPara("template_code");
        String coupon_code=getPara("coupon_code");
        String reserve_code=getPara("reserve_code");
        String short_code=getPara("short_code");
        _log.info("请求查询参数:streaming_no=" + streaming_no + ",sp_account="
                + sp_account + ",send_number=" + send_number
                + ",day_id_from=" + day_id_from + ",day_id_to="
                + day_id_to );
        if (StringUtils.isBlank(send_number)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "发送号码不能为空");
            renderJson();
            return;
        }
        if (StringUtils.isBlank(busi_type)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "业务类型不能为空");
            renderJson();
            return;
        }
        if (StringUtils.isBlank(day_id_from)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "开始日期不能为空");
            renderJson();
            return;
        }
        if (StringUtils.isBlank(day_id_to)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "结束日期不能为空");
            renderJson();
            return;
        }
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("send_parent_account:" + send_parent_account);
        strBuf.append(" AND busi_type:" + busi_type);
        day_id_from = StringUtils.trim(day_id_from);
        day_id_to = StringUtils.trim(day_id_to);
        strBuf.append(" AND (day_id:[");
        strBuf.append(day_id_from);
        strBuf.append(" TO ");
        strBuf.append(StringUtils.trim(day_id_to));
        strBuf.append("]) ");
        if (StringUtils.isNotBlank(send_number)) {
            strBuf.append(" AND send_number:" + send_number);
        }
        if (StringUtils.isNotBlank(template_code)) {
            strBuf.append(" AND template_code:" + template_code);
        }
        if (StringUtils.isNotBlank(coupon_code)) {
            strBuf.append(" AND coupon_code:" + coupon_code);
        }
        if (StringUtils.isNotBlank(reserve_code)) {
            strBuf.append(" AND reserve_code:" + reserve_code);
        }
        if (StringUtils.isNotBlank(short_code)) {
            strBuf.append(" AND short_code:" + short_code);
        }
        String querySql = strBuf.toString();
        _log.info("solr查询语句:" + querySql);
        try {
            setAttr("streaming_no", streaming_no);
            SolrDocumentList docList = Sms2SolrUtil.searchBusinessTypeByDay(querySql);
            setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
            List<BusinessTypeByDayBean> list=HbaseClientUtil.querySmsDayByRowKeys(SolrObject.toIDList(docList));
            setAttr("result_msg", EnumAPIResultCode.SUCCESS.getName());
            setAttr("business_sum",list);
        } catch (Exception e) {
            _log.info("solr查询异常:" + e);
            setAttr("result_code", EnumAPIResultCode.SYSTEM_ERROR.getValue());
            setAttr("result_msg", EnumAPIResultCode.SYSTEM_ERROR.getName());
        }
        renderJson();
        return;
    }
}
